<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: black;
            left: 100px;
            top: 100px;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <button id="btn">按我移动</button>
    <script>
        // 函数节流，一个函数执行一次后，只有大于设定执行周期后才允许执行第二次
        var lock = true;
        //标识量，指示当前盒子在左面还是右面
        var pos = 1;
        var box = document.getElementById('box')
        var btn = document.getElementById('btn')

        // 事件监听
        btn.onclick = function() {
            // 首先检查锁是否关闭，如果锁是关闭状态，则不执行
            if (!lock) return;
            box.style.transition = 'all 2s linear 0s';
            if (pos == 1) {
                //瞬间移动但是因为有过度所以是动画
                box.style.left = '1000px';
                pos = 2;
            } else if (pos == 2) {
                box.style.left = '100px';
                pos = 1;
            }
            // 关锁
            lock = false;

            // 指定毫秒数后开锁
            setTimeout(function() {
                lock = true
            }, 2000);
        }
    </script>
</body>

</html>